package com.example.mydemo.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
    private static void copyFile(InputStream in, File target) {
        if (target != null) {
            boolean copySuccess = true;
            OutputStream out = null;

            try {
                out = new FileOutputStream(target);
                byte[] buffer = new byte[1024];

                int counter;
                while((counter = in.read(buffer)) != -1) {
                    out.write(buffer, 0, counter);
                }
            } catch (IOException var14) {
                var14.printStackTrace();
                copySuccess = false;
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }

                    if (in != null) {
                        in.close();
                    }
                } catch (Exception var13) {
                    var13.printStackTrace();
                }

            }

            if (!copySuccess) {
                target.delete();
            }

        }
    }
   public static void copyModel(Context context, String modelPath, String modelName) {
        if (!isModelExists(context, modelPath, modelName)) {
            File modelDir = new File(modelPath);
            if (!modelDir.exists()) {
                modelDir.mkdirs();
            }

            AssetManager assetManager = context.getAssets();

            try {
                String[] children = assetManager.list(modelName);
                String[] var6 = children;
                int var7 = children.length;

                for(int var8 = 0; var8 < var7; ++var8) {
                    String child = var6[var8];
                    File childFile = new File(modelPath + "/" + child);
                    child = modelName + "/" + child;
                    if (!childFile.exists() || (long)assetManager.open(child).available() != childFile.length()) {
                        copyFile(assetManager.open(child), childFile);
                    }
                }


            } catch (Exception var11) {
                var11.printStackTrace();
            }
        }

    }
    private static boolean isModelExists(Context context, String modelPath, String modelName) {
        AssetManager assetManager = context.getAssets();

        try {
            String[] children = assetManager.list(modelName);
            String[] var5 = children;
            int var6 = children.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                String child = var5[var7];
                File childFile = new File(modelPath + "/" + child);
                child = modelName + "/" + child;
                if (!childFile.exists() || (long)assetManager.open(child).available() != childFile.length()) {
                    return false;
                }
            }
        } catch (IOException var10) {
            var10.printStackTrace();
        }

        return true;
    }
    public static List<File> getMp4Files(String folderPath) {
        List<File> mp4Files = new ArrayList<>();
        File folder = new File(folderPath);

        if (folder.exists() && folder.isDirectory()) {
            searchMp4Files(folder, mp4Files);
        }

        return mp4Files;
    }

    private static void searchMp4Files(File currentFolder, List<File> mp4Files) {
        File[] files = currentFolder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    searchMp4Files(file, mp4Files);
                } else if (file.isFile() && file.getName().endsWith(".mp4")) {
                    mp4Files.add(file);
                }
            }
        }
    }
    public static boolean writeToTxt(String content) {
        return writeToTxt("demoLog.txt",content);
    }
    public static boolean writeToTxt( String fileName,String content) {
        // 检查外部存储是否可写
        if (!isExternalStorageWritable()) {
            return false;
        }

        // 获取外部存储的公共目录
        File externalDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        File folder = new File(externalDir, "MyLog");

        // 如果文件夹不存在则创建
        if (!folder.exists()) {
            if (!folder.mkdirs()) {
                return false;
            }
        }

        File file = new File(folder, fileName);

        try (FileWriter writer = new FileWriter(file,true)) {
            writer.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }
    public static List<File> getAllFilesInTestFolder() {
        List<File> allFiles = new ArrayList<>();
        File externalDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        File testFolder = new File(externalDir, "imgFile");

        if (testFolder.exists() && testFolder.isDirectory()) {
            searchAllFiles(testFolder, allFiles);
        }

        return allFiles;
    }

    private static void searchAllFiles(File currentFolder, List<File> allFiles) {
        File[] files = currentFolder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    searchAllFiles(file, allFiles);
                } else if (file.isFile()) {
                    allFiles.add(file);
                }
            }
        }
    }
    public static Bitmap fileToBitmap(File file){
        if (file == null || !file.exists()) {
            return null;
        }

        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

        return bitmap;

    }
}
